package edu.ncsu.csc.ip.sender;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class SenderThread extends Thread {

	private DatagramSocket socket = null;
	private DatagramPacket packet = null;
	private StopNWaitProtocol protocol = null;

	public SenderThread(String name, DatagramPacket packet,
			StopNWaitProtocol protocol) throws IOException {
		super(name);
		this.protocol = protocol;
		this.packet = packet;

	}

	public void run() {

		try {

			// get a datagram socket
			socket = new DatagramSocket();
			// receive request
			socket.send(packet);
			protocol.packetSent(packet);
			byte[] ackBuffer = new byte[StopNWaitProtocol.ACK_HEADER_SIZE];
			DatagramPacket ackPacket = new DatagramPacket(ackBuffer,
					ackBuffer.length);
			socket.receive(ackPacket);
			protocol.ackReceived(ackPacket);
		} catch (IOException e) {
			e.printStackTrace();
		}

		socket.close();
	}

	public DatagramSocket getSocket() {
		return socket;
	}

	public void setSocket(DatagramSocket socket) {
		this.socket = socket;
	}

	public DatagramPacket getPacket() {
		return packet;
	}

	public void setPacket(DatagramPacket packet) {
		this.packet = packet;
	}

}
